package com.znsb.msfq.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.znsb.msfq.app.ZnsbApp;

import android.graphics.Bitmap;
import android.os.Environment;
import android.text.TextUtils;

/**
 * @ClassName: FileUtils
 * @Description: TODO
 * @author: Administrator
 * @date: 2015年4月2日 下午1:48:33
 */
public class SDCardUtils {
    public final static String CACHE = "/zsfq/picture";

    private SDCardUtils() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    /**
     * 获取Sdcard卡路径
     *
     * @return SDPath
     */
    public static String getSDPath() {
        File sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory();
        } else {
            sdDir = ZnsbApp.mContext.getCacheDir();
        }
        return sdDir.toString();
    }

    public static File getFilePath(String filePath,
                                   String fileName) {
        File file = null;
        makeRootDirectory(filePath);
        try {
            file = new File(filePath + fileName);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return file;
    }

    public static void makeRootDirectory(String filePath) {
        File file = null;
        try {
            file = new File(filePath);
            if (!file.exists()) {
                file.mkdir();
            }
        } catch (Exception e) {

        }
    }

    //创建文件夹及文件
    public void CreateText(String filePath,
                           String fileName) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            try {
                //按照指定的路径创建文件夹
                file.mkdirs();
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
        File dir = new File(filePath + fileName);
        if (!dir.exists()) {
            try {
                //在指定的文件夹中创建文件
                dir.createNewFile();
            } catch (Exception e) {
            }
        }

    }

    /**
     * 获取img存放路径
     */
    public static String getCacheDir() {

        String imgDir = getSDPath() + CACHE;
        File fileImgDir = new File(imgDir);
        if (!fileImgDir.exists()) {
            fileImgDir.mkdirs();
        }
        return fileImgDir.getAbsolutePath();
    }

    public static File getCacheDirFile() {
        String path = getCacheDir();
        File fileDir = new File(path);
        return fileDir;
    }

    /**
     * 获取img存放路径
     *
     * @return SDPath
     */
    public static String getDCIMDir() {

        String imgDir = getSDPath() + "/DCIM/Camera";
        File fileImgDir = new File(imgDir);
        if (!fileImgDir.exists()) {
            fileImgDir.mkdirs();
        }
        return fileImgDir.getAbsolutePath();
    }

    public static void saveBitmapToSdcard(File file, Bitmap bitmap) {
        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
        try {
            fOut.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取系统存储路径
     *
     * @return
     */
    public static String getRootDirectoryPath() {
        return Environment.getRootDirectory().getAbsolutePath();
    }


    /**
     * 删除发帖时临时创建的压缩文件
     *
     * @return
     */
    public static boolean deleteCompressFolder() {
        String imgDir = getSDPath() + "/ChatDocument/Publish";
        File file = new File(imgDir);
        if (!file.exists()) {
            return false;
        } else {
            if (file.isFile()) {
                // 为文件时调用删除文件方法
                return deleteFile(imgDir);
            } else {
                // 为目录时调用删除目录方法
                return deleteDirectory(imgDir);
            }
        }
    }

    /**
     * 删除单个文件
     *
     * @param filePath 被删除文件的文件名
     * @return 文件删除成功返回true，否则返回false
     */
    public static boolean deleteFile(String filePath) {
        if (TextUtils.isEmpty(filePath)) return false;
        File file = new File(filePath);
        if (file.isFile() && file.exists()) {
            return file.delete();
        }
        return false;
    }

    /**
     * 删除文件夹以及目录下的文件
     *
     * @param filePath 被删除目录的文件路径
     * @return 目录删除成功返回true，否则返回false
     */
    public static boolean deleteDirectory(String filePath) {
        boolean flag = false;
        // 如果filePath不以文件分隔符结尾，自动添加文件分隔符
        if (!filePath.endsWith(File.separator)) {
            filePath = filePath + File.separator;
        }
        File dirFile = new File(filePath);
        if (!dirFile.exists() || !dirFile.isDirectory()) {
            return false;
        }
        flag = true;
        File[] files = dirFile.listFiles();
        // 遍历删除文件夹下的所有文件(包括子目录)
        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()) {
                // 删除子文件
                flag = deleteFile(files[i].getAbsolutePath());
                if (!flag)
                    break;
            } else {
                // 删除子目录
                flag = deleteDirectory(files[i].getAbsolutePath());
                if (!flag)
                    break;
            }
        }
        if (!flag)
            return false;
        // 删除当前空目录
        return dirFile.delete();
    }
}
